package org.darkforest.admin.controller;

import org.darkforest.common.services.GoodsClassRedisService;
import org.darkforest.common.services.GoodsRecommendService;
import org.darkforest.model.GoodsClass;
import org.darkforest.model.GoodsRecommend;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by Administrator on 2016/1/25.
 */
@RequestMapping(value = "operation")
@Controller
public class GoodsRecommendController {

    @Autowired
    private GoodsRecommendService goodsRecommendService;

    @Autowired
    private GoodsClassRedisService goodsClassRedisService;


    /**
     * 分类推荐商品分类列表
     * @param start
     * @param gcId
     * @return
     */
    @RequestMapping(value = "goodsRecommendList")
    public ModelAndView goodsRecommendList(@RequestParam(required = false, defaultValue = "1") int start,
                                           @RequestParam(required = false, defaultValue = "") String goodsName,
                                           @RequestParam(required = false, defaultValue = "0") String gcId){
        ModelAndView res = new ModelAndView("operation/recommend/goods_recommend");
        GoodsRecommend goodsRecommend = new GoodsRecommend();
        goodsRecommend.setStart(start);
        if(!"0".equals(gcId)){
            goodsRecommend.setGcId1(Integer.parseInt(gcId));
        }
        if(!"".equals(goodsName)){
            goodsRecommend.setGoodsName(goodsName);
        }
        goodsRecommend.setResultList(goodsRecommendService.findListByCondition(goodsRecommend));
        List<GoodsClass> goodsClassList = goodsClassRedisService.getGoodsClassList(null);
        res.addObject("page",goodsRecommend);
        res.addObject("classList",goodsClassList);
        return res;
    }


    /**
     * 批量删除分类推荐商品
     * @param recommendId
     * @param type 操作类型 1 失效 2删除
     * @return
     */
    @RequestMapping(value = "updateGoodsRecommend")
    public @ResponseBody AjaxReturnData updateGoodsRecommend(Integer recommendId,Integer type,Integer state){

        if(type==1){
            GoodsRecommend goodsRecommend = new GoodsRecommend();
            goodsRecommend.setRecommendId(recommendId);
            goodsRecommend.setRecommendState(state==1?2:1);
            goodsRecommendService.updateByPrimaryKeySelective(goodsRecommend);
        }else{
            goodsRecommendService.deleteByPrimaryKey(recommendId);
        }
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        ajaxReturnData.setResult(true);
        ajaxReturnData.setMessage("操作成功");
        ajaxReturnData.setUrl("operation/goodsRecommendList");
        return  ajaxReturnData;
    }


    @RequestMapping(value = "doSetRecommendGoods")
    public @ResponseBody AjaxReturnData doSetRecommendGoods(Integer goodsCommonid){
        String url = "operation/goodsRecommendList";
        AjaxReturnData ajaxReturnData =   goodsRecommendService.doSetRecommendGoods(goodsCommonid,url);
        return  ajaxReturnData;
    }




}
